<?php
//declare (strict_types = 1);

namespace app\index\controller;


use app\model\adminModel;
use app\model\usersModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Login
{
    public function index()
    {
        return View::fetch('login/index');
    }

    public function loginSave()
    {
        $params = Request::param();

        $validate = Validate::rule([
            'email|邮箱' => 'require|min:2|max:100',
            'pwd|密码' => 'require|min:1|max:6',
            'checked_code|验证码' => 'require'
        ]);
        if(!$validate->check($params)){
            echo $validate->getError();
            exit();
        }
        //判断验证码是否正确
        session_start();
        if($_SESSION['checked_code'] != $params['checked_code']){
            echo "<script>alert('验证码错误');</script>";
            echo "<script>location.href='/index.php?s=index/login/index'</script>";
            exit();
        }

        //判断账号密码是否正确
        if($admin = adminModel::where('admin_email','=',$params['email'])->find()){//管理员登录

            if($admin && $params['pwd'] == $admin['admin_pwd']){
                $_SESSION['id'] = $admin['admin_id'];
                $_SESSION['email'] = $admin['admin_email'];
                $_SESSION['name'] = $admin['admin_name'];
                echo "<script>alert('登录成功');</script>";
                echo "<script>location.href='/index.php?s=admin/questionnaire/index'</script>";
            }else{
                echo "<script>alert('登录失败,邮箱或者密码不正确');</script>";
                echo "<script>location.href='/index.php?s=index/login/index'</script>";
            }

        }else if ($user = usersModel::where('user_email','=',$params['email'])->find()){//用户登录

            if($user && $params['pwd'] == $user['user_pwd']){
                $_SESSION['id'] = $user['user_id'];
                $_SESSION['email'] = $user['user_email'];
                $_SESSION['name'] = $user['user_name'];
                echo "<script>alert('登录成功');</script>";
                echo "<script>location.href='/index.php?s=index/index/home'</script>";
            }else{
                echo "<script>alert('登录失败,邮箱或者密码不正确');</script>";
                echo "<script>location.href='/index.php?s=index/login/index'</script>";
            }

        }
    }

    public function loginOut()
    {
        $_SESSION['email'] = null;
        $_SESSION['name'] = null;

        echo "<script>alert('退出成功');</script>";
        echo "<script>location.href='/index.php?s=index/login/index'</script>";
    }

    public function checked_code()
    {
        session_start();

        $image = \imagecreate(85,35);//创建底片
        $imageColor = \imagecolorallocate($image,115,180,244);//设置底片颜色

        //随机生成四位数字
        $checked_code = '';
        for($i = 1;$i <=4;$i++){
            $str = '0123456789';//设一串数字
            $font = substr($str,mt_rand(0,mb_strlen($str)),1);//指定字符串从0开始随机生成四位数字
            $checked_code .= $font;//存储生成的字符串

            $strColor = \imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));//设置数字颜色
            \imagestring($image,6,15*$i,8,$font,$strColor);//设置数字的位置

        }
        //存储生成的四位随机数
        $_SESSION['checked_code'] = $checked_code;

        header("Content-type: image/png");//设置为png格式
        imagepng($image);// 以 PNG 格式将图像输出到浏览器
        imagedestroy($image);//销毁图像
    }

    public function enroll()
    {
        return View::fetch('login/enroll');
    }

    public function enrollSave()
    {
        $params = Request::param();

        $validate = Validate::rule([
            'user_name|用户名' => 'require|min:1|max:6',
            'user_email|邮箱' => 'require|min:2|max:100',
            'user_pwd|密码' => 'require|min:1|max:6',
            'pwds|确认密码' => 'require|min:1|max:6'
        ]);
        if(!$validate->check($params)){
            echo $validate->getError();
            exit();
        }
        if($params['user_pwd'] != $params['pwds']){
            echo "<script>alert('两次密码不一致,请重新输入');</script>";
            echo "<script>location.href='/index.php?s=index/login/enroll'</script>";
            exit();
        }

        $params['add_time'] = time();
        $params['update_time'] = time();

        $result = usersModel::create($params);
        if($result){
            echo "<script>alert('注册成功');</script>";
            echo "<script>location.href='/index.php?s=index/login/index'</script>";
        }else{
            echo "<script>alert('注册失败');</script>";
            echo "<script>location.href='/index.php?s=index/login/enroll'</script>";
        }
    }
}